<%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2022/6/29
  Time: 14:59
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script src="${pageContext.request.contextPath}/static/layui/layui.js"></script>
    <link href="${pageContext.request.contextPath}/static/layui/css/layui.css" rel="stylesheet">
</head>
<body>
<!-- 模糊查询框 -->
<form class="layui-form" action="" id="searchForm">
    <div class="layui-inline">
        <label class="layui-form-label">问题</label>
        <div class="layui-input-inline">
            <input type="text" id="question" name="question"  placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">ID</label>
        <div class="layui-input-inline">
            <input type="text" id="id" name="id"  placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <div class="layui-input-inline">
            <button class="layui-btn" lay-submit lay-filter="formDemo" id="reserch">查询</button>
            <button type="reset" class="layui-btn layui-btn-primary" id="search_reset">重置</button>
        </div>
    </div>
</form>
<%--头部工具栏--%>
<script type="text/html" id="top">
    <div class="layui-btn-container">
        <button type="button" class="layui-btn" lay-event="add">添加</button>
        <button type="button" class="layui-btn-danger layui-btn" lay-event="delete">批量删除</button>
    </div>
</script>
<%--行内工具栏--%>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
</script>
<table id="demo" lay-filter="test"></table>
<%--//添加录入框--%>
<form class="layui-form" id="insertQuestion" style="display: none "> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
<%--    <div class="layui-inline">--%>
<%--        <label class="layui-form-label">问题</label>--%>
<%--        <div class="layui-input-block" style=" width: 100%">--%>
<%--            <input type="text" name="question" placeholder="请输入" autocomplete="off" class="layui-input">--%>
<%--        </div>--%>
<%--    </div>--%>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">问题</label>
        <div class="layui-input-block">
            <textarea name="question" placeholder="请输入问题" class="layui-textarea"></textarea>
        </div>
    </div>
<%--    <div class="layui-inline">--%>
<%--        <label class="layui-form-label">选项A</label>--%>
<%--        <div class="layui-input-block" style=" width: 200px">--%>
<%--            <input type="text" name="option_a" placeholder="请输入" autocomplete="off" class="layui-input">--%>
<%--        </div>--%>
<%--    </div>--%>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项A</label>
        <div class="layui-input-block">
            <textarea name="option_a" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项B</label>
        <div class="layui-input-block">
            <textarea name="option_b" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项C</label>
        <div class="layui-input-block">
            <textarea name="option_c" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项D</label>
        <div class="layui-input-block">
            <textarea name="option_d" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>
<%--    <div class="layui-inline"  >--%>
<%--        <label class="layui-form-label">类型</label>--%>
<%--        <div class="layui-input-block" style=" width: 200px">--%>
<%--            <input type="text" name="type" placeholder="请输入" autocomplete="off" class="layui-input" >--%>
<%--        </div>--%>
<%--    </div>--%>
    <div class="layui-form-item" >
        <label class="layui-form-label">类型</label>
        <div class="layui-input-block">
            <input type="radio" name="type" value="1" title="FPA前15道题" checked="">
            <input type="radio" name="type" value="2" title="FPA后15道题">
        </div>
    </div>
        <div class="layui-inline" style="display: none">
            <label class="layui-form-label">创建人</label>
            <div class="layui-input-block" style=" width: 200px">
                <input type="text" name="create_by" placeholder="请输入" autocomplete="off" class="layui-input" value="${userinfo.username}">
            </div>
        </div>
    <div class="layui-inline">
        <label class="layui-form-label">状态</label>
<%--        <div class="layui-input-block" style=" width: 200px">--%>
<%--            <input type="text" name="status" placeholder="请输入" autocomplete="off" class="layui-input" >--%>
<%--        </div>--%>
        <div class="layui-input-block">
            <input type="radio" name="status" value="0" title="正常" checked="">
            <input type="radio" name="status" value="1" title="停用">
        </div>
    </div>
    <div class="layui-input-block">
        <button type="button" class="layui-btn " lay-filter="insert" lay-submit>确认添加</button>
        <button type="button" class="layui-btn layui-btn-danger" lay-filter="remove">清空</button>
    </div>
</form>
<%--  修改页面--%>
<form class="layui-form" id="updateUsers" style="display: none" lay-filter="updateForm"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <div class="layui-inline" style="display: none">
        <label class="layui-form-label">ID</label>
        <div class="layui-input-block" style=" width: 200px">
            <input type="text" name="id" placeholder="请输入" autocomplete="off" class="layui-input" >
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">问题</label>
        <div class="layui-input-block">
            <textarea name="question" placeholder="请输入问题" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项A</label>
        <div class="layui-input-block">
            <textarea name="option_a" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项B</label>
        <div class="layui-input-block">
            <textarea name="option_b" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项C</label>
        <div class="layui-input-block">
            <textarea name="option_c" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项D</label>
        <div class="layui-input-block">
            <textarea name="option_d" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>
<%--    <div class="layui-inline">--%>
<%--        <label class="layui-form-label">类型</label>--%>
<%--        <div class="layui-input-block" style=" width: 200px">--%>
<%--            <input type="text" name="type" placeholder="请输入" autocomplete="off" class="layui-input">--%>
<%--        </div>--%>
<%--    </div>--%>
    <div class="layui-form-item" >
        <label class="layui-form-label">类型</label>
        <div class="layui-input-block">
            <input type="radio" name="type" value="1" title="FPA前15道题" checked="">
            <input type="radio" name="type" value="2" title="FPA后15道题">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">状态</label>
<%--        <div class="layui-input-block" style=" width: 200px">--%>
<%--            <input type="text" name="status" placeholder="请输入" autocomplete="off" class="layui-input">--%>
<%--        </div>--%>
        <div class="layui-input-block">
            <input type="radio" name="status" value="0" title="正常" checked="">
            <input type="radio" name="status" value="1" title="停用">
        </div>
    </div>
    <div class="layui-inline" style="display: none">
        <label class="layui-form-label">修改人</label>
        <div class="layui-input-block" style=" width: 200px">
            <input type="text" name="update_by" value="${userinfo.username}" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-input-block">
        <button type="button" class="layui-btn "  lay-submit lay-filter="updateUsers">确认修改</button>
        <button type="button" class="layui-btn layui-btn-danger" lay-filter="remove">清空</button>
    </div>
</form>
</body>
<script>
    layui.use(['table','form','jquery'], function(){
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;
        //第一个实例
        table.render({
            elem: '#demo'
            ,height: 520
            ,url: '${pageContext.request.contextPath}/question/selectQuestion.do' //数据接口
            ,page: true //开启分页
            ,limit:10
            ,limits:[5,10,15,20,25,30,50]
            ,autoSort: true //禁用前端的自动排序，开启后端的服务器排序，根据客户需求
            ,toolbar: '#top'//开启顶部工具栏
            ,cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'id', title: 'ID', width:70, sort: true, fixed: 'left'}
                ,{field: 'question', title: '问题', width:120,sort: true}
                ,{field: 'option_a', title: '选项A', width:120}
                ,{field: 'option_b', title: '选项B', width:120}
                ,{field: 'option_c', title: '选项C', width: 120}
                ,{field: 'option_d', title: '选项D', width: 120}
                ,{field: 'status', title: '状态', width: 70,
                    templet: function (d) {
                        if (d.status == "0")
                            return '<button class="layui-btn layui-btn-xs">正常</button>';
                        else if (d.status == "1")
                            return '<button class="layui-btn layui-btn-danger layui-btn-xs">停用</button>';
                    }
                }
                ,{field: 'dictionary_name', title: '类型', width: 120,
                    templet: function (d) {
                        return d.dictionary.dictionary_name
                    }
                }
                ,{field: 'create_by', title: '创建人', width: 120}
                ,{field: 'create_time', title: '创建时间', width: 180}
                ,{field: 'update_by', title: '修改人', width: 120}
                ,{field: 'update_time', title: '修改时间', width: 180}
                ,{field: 'flag', title: '标志', width: 70}
                ,{fixed: 'right', title: '操作',width: 200, align:'center', toolbar: '#barDemo'}
            ]]
        });
        <!--模糊查询-->
        form.on('submit(formDemo)', function(data){
            console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
            console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
            console.log(data.field)
            table.reload('demo',{
                where: {
                    question: data.field.question
                    ,id: data.field.id
                }
                ,page: {
                    curr: 1
                }
            });
            return false;
        });
        //监听头部重置
        $("#search_reset").click(function () {
            table.reload('demo', {
                where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    question: "" //第一个查询条件
                    ,id:"" //第二个查询条件
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        })
        //监听头部工具栏事件
        table.on('toolbar(test)', function(obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据
            console.log(data)
            switch (obj.event) {
                //添加一行
                case 'add':
                    //弹出对话框填写添加信息
                    layer.open({
                        title:"添加问题",
                        type:1,
                        content:$("#insertQuestion"),
                        area:['500px','500px']
                    })
                    //监听submit提交
                    form.on('submit(insert)', function(data){
                        console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
                        console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
                        console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
                        $.ajax({
                            url:"${pageContext.request.contextPath}/question/addQuestion.do",
                            data:data.field,
                            dataType:"json",
                            type:"post",
                            success:function (a){
                                if (a.code==0){
                                    layer.closeAll()
                                    table.reload('demo',{
                                        page:{
                                            curr:1
                                        }
                                    })
                                }
                                layer.msg(a.msg)
                            }
                        })
                        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                    });
                    break;
                //    批量删除
                case 'delete':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                    } else {
                        var id=[]
                        for (var i=0;i<data.length;i++){
                            id[i]=data[i].id;
                        }
                        console.log(id.join(","))
                        layer.confirm('真的删除么', function(index){
                            //确定后进入后台删除数据
                            $.ajax({
                                url: "${pageContext.request.contextPath}/question/deleteQuestion.do",
                                data:{
                                    id:id.join(",")
                                },
                                dataType:"json",
                                type: "post",
                                success:function (a){
                                    if (a.code==0){
                                        //关闭所有对话框
                                        layer.closeAll();
                                        table.reload('demo',{
                                            page:{
                                                curr: 1
                                            }
                                        })
                                    }
                                }
                            })
                        })
                    }
                    break;
            }
        })
        //监听行工具事件
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'delete') {
                layer.confirm('确定删除行吗', function (index) {
                    console.log(data.id)
                    //删除数据库中
                    $.ajax({
                        url: "${pageContext.request.contextPath}/question/deleteQuestion.do",
                        type: "POST",
                        dataType: "JSON",
                        data: {id: data.id},
                        success: function (data) {
                            if (data.code == 0) {
                                //关闭对话框
                                layer.closeAll(); //疯狂模式，关闭所有层
                                //刷新表格数据
                                table.reload('demo');

                            }
                            layer.msg(data.msg);
                        }

                    });
                });
            }else if (layEvent === 'edit'){
                console.log(data.id+"!!!!!!!!")
                layer.open({
                    title:"修改数据",
                    type:1,
                    content:$("#updateUsers"),
                    area:['500px','500px']
                })
                form.val("updateForm", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
                    "id": obj.data.id // "name": "value"
                    ,"question": obj.data.question
                    ,"option_a":obj.data.option_a
                    ,"option_b":obj.data.option_b
                    ,"option_c":obj.data.option_c
                    ,"option_d":obj.data.option_d
                    ,"type":obj.data.type
                    ,"status":obj.data.status
                    // ,"update_by":obj.data.update_by
                });
                form.on('submit(updateUsers)', function(data){
                    console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
                    console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
                    console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
                    $.ajax({
                        url:"${pageContext.request.contextPath}/question/updateQuestion.do",
                        data:data.field,
                        dataType:"json",
                        type:"post",
                        success:function (a){
                            if (a.code==0){
                                layer.closeAll()
                                table.reload('demo')
                            }
                            layer.msg(a.msg)
                        }
                    })
                    return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                });
            }
        });
        $("#remove").click(function (){
            table.reload("demo",{
                where:{
                    id: ""
                    ,question:""
                    ,option_a:""
                    ,option_b:""
                    ,option_c:""
                    ,option_d:""
                    ,type:""
                    ,status:""
                    ,update_by:""
                },
                page:{
                    curr:1
                }
            })
        })
    });
</script>
</html>
